/** 
 * 动漫视频链接控制器模块
 */
module controller.AnimationVlinkController;

import std.conv : to;

import vibe.http.server : HTTPServerRequest, HTTPServerResponse;
import vibe.data.json : Json;

import model.AnimationVlink;
import repository.AnimationLinksRepo;
import utils.RespData : RespData;
import utils.ObjMapping : ObjMapping;
import utils.SecurityVerification : signValidator;

AnimationLinksRepo _alRepo;

static this ()
{
    _alRepo = new AnimationLinksRepo();
}

static ~this ()
{
    destroy(_alRepo);
}

/** 
* 获取一个动漫视频链接
*/
void getOneAnimationLink (HTTPServerRequest req, HTTPServerResponse resp)
{
    string idStr = req.params.get("id", "");
    if (idStr == "")
    {
        resp.writeJsonBody(RespData.newData(400, "错误的请求"));
        return;
    }
    uint id = to!uint(idStr);

    auto alink = _alRepo.getOneAnimationLink(id);
    if (alink is null)
    {
        resp.writeJsonBody(RespData.newData(404, "未找到"), 404);
        return;
    }

    auto respData = RespData.newData!(AnimationVlinkDto)(200, "ok", ObjMapping.mappingToObject!(
            AnimationVlinkDto, AnimationVlink)(alink));
    resp.writeJsonBody(respData);
}

/** 
* 添加动漫视频链接
*/
void addAnimationLink (HTTPServerRequest req, HTTPServerResponse resp)
{
    Json json = req.json;
    string animation_id = json["teleplay_id"].to!string();
    string name = json["name"].to!string();
    string url = json["url"].to!string();
    string real_url = json["real_url"].to!string();

    string sign = json["sign"].to!string();

    import std.format : format;

    string reqParamsStr = format("%s%s", name, url);
    if (!signValidator(reqParamsStr, sign))
    {
        resp.writeJsonBody(RespData.newData(403, "签名不正确！"), 403);
        return;
    }

    AnimationVlinkDtoFromCreate al = new AnimationVlinkDtoFromCreate(animation_id, name, url, real_url);
    bool res = _alRepo.addAnimationLink(
        ObjMapping.mappingToObject!(AnimationVlink, AnimationVlinkDtoFromCreate)(
            al));
    if (!res)
    {
        resp.writeJsonBody(RespData.newData(500, "error"), 500);
        return;
    }
    resp.writeJsonBody(RespData.newData(200, "ok"), 201);
}

/** 
* 删除一个动漫视频链接
*/
void removeAnimationLink (HTTPServerRequest req, HTTPServerResponse resp)
{
    string idStr = req.params.get("id", "");
    string sign = req.query.get("sign", "");
    if (idStr == "" || sign == "")
    {
        resp.writeJsonBody(RespData.newData(400, "错误的请求"));
        return;
    }
    uint id = to!uint(idStr);

    if (!signValidator(idStr, sign))
    {
        resp.writeJsonBody(RespData.newData(403, "签名不正确！"), 403);
        return;
    }

    bool res = _alRepo.removeAnimationLink(id);
    if (!res)
    {
        resp.writeJsonBody(RespData.newData(500, "error"), 500);
        return;
    }
    resp.writeJsonBody(RespData.newData(200, "ok"));
}

/** 
* 编辑动漫视频链接
*/
void editAnimationLink (HTTPServerRequest req, HTTPServerResponse resp)
{
    string idStr = req.params.get("id", "");
    if (idStr == "")
    {
        resp.writeJsonBody(RespData.newData(400, "错误的请求"));
        return;
    }
    uint id = to!uint(idStr);

    Json json = req.json;
    string name = json["name"].to!string();
    string url = json["url"].to!string();
    string real_url = json["real_url"].to!string();

    string sign = json["sign"].to!string();

    import std.format : format;

    string reqParamsStr = format("%s%s", name, url);
    if (!signValidator(reqParamsStr, sign))
    {
        resp.writeJsonBody(RespData.newData(403, "签名不正确！"), 403);
        return;
    }

    AnimationVlinkDtoFromUpdate al = new AnimationVlinkDtoFromUpdate(name, url, real_url);
    bool res = _alRepo.editAnimationLink(id, ObjMapping.mappingToObject!(
            AnimationVlink, AnimationVlinkDtoFromUpdate)(
            al));
    if (!res)
    {
        resp.writeJsonBody(RespData.newData(500, "error"), 500);
        return;
    }
    resp.writeJsonBody(RespData.newData(200, "ok"));
}
